local love = require 'love'
local fontHandler = require('assets-handler.font-handler')
local numToChinese = require('utils.num-to-chinese')

local setColor = love.graphics.setColor
local drawRectangle = love.graphics.rectangle
local printg = love.graphics.print

local width = Width
local height = Height
local padding = Padding

-- 信息栏模块
local messages = {}

local infoBox = {
    floor = 0,
    ceil = 0,
    left = 0,
    width = 0,
    height = 0,
    color = {0, 0, 0, 0.3},
    active = false
}

local infoText = {
    floor = 0,
    fontSize = 14,
    fontHeight = 0,
    maxVisibleMessages = 4,
    fadeFactor = 0.2,  -- 每行之间的透明度衰减因子
    displayOffset = 0,  -- 用于显示历史消息的偏移量
    change = 0,
    color = {1, 1, 1, 1}
}

-- 初始化信息栏
local function init()
    fontHandler.setFont(infoText.fontSize)
    infoText.fontHeight = fontHandler.getFontHeight() + 4  -- 行高，加4像素间距

    local handCardsHeight = height / 6
    local handCardsY = height - padding - handCardsHeight - height / 20 * 2

    infoBox.left = 0
    infoBox.width = width - infoBox.left
    infoBox.height = infoText.fontHeight * infoText.maxVisibleMessages
    infoBox.floor = handCardsY
    infoBox.ceil = infoBox.floor - infoBox.height

    infoText.floor = infoBox.floor
end

-- 添加新消息
local function addMessage(text, turn)
    if turn == nil then
        table.insert(messages, {
        time = os.clock(),
        text = text
        })
    else
        text = "第" .. numToChinese.numberToChinese(turn) .. "回合" .. text
        table.insert(messages, {
            time = os.clock(),
            text = text
        })
    end
    infoText.displayOffset = 0  -- 新消息添加后重置显示偏移
    -- print(text)
end

-- 绘制信息栏
local function drawInfoBar()
    if infoBox.active then
        -- 绘制背景
        setColor(infoBox.color)
        drawRectangle("fill", infoBox.left, infoBox.ceil, infoBox.width, infoBox.height)

        fontHandler.setFont(infoText.fontSize)
        -- 绘制消息（最新消息在最底部）
        for i = 1, infoText.maxVisibleMessages do
            local msgIndex = #messages - (i - 1) - infoText.displayOffset
            if msgIndex >= 1 and msgIndex <= #messages then
                local message = messages[msgIndex]
                local alpha = 1 - ((i - 1) * infoText.fadeFactor)

                -- 计算位置（从底部开始）
                local yPos = infoText.floor - infoText.fontHeight - (infoText.fontHeight * (i - 1))

                -- 右对齐计算
                local textWidth = fontHandler.getFontWidth(message.text)
                -- 绘制文本
                setColor(1, 1, 1, alpha)
                printg(message.text,
                    width - textWidth - padding,  -- 右对齐
                    yPos)
            end
        end
    end
end

local function keypressed(key)
    if key == "up" then
        infoText.change = 1
    elseif key == "down" then
        infoText.change = - 1
    end

    if infoText.change ~= 0 then
        infoText.displayOffset = math.max(0, math.min(#messages - infoText.maxVisibleMessages, infoText.displayOffset + infoText.change))
    end
end

local function inArea(x, y)
    return x >= infoBox.left and x <= infoBox.left + infoBox.width and y >= infoBox.ceil and y <= infoBox.floor
end

return{
    init = init,
    drawInfoBar = drawInfoBar,
    addMessage = addMessage,
    inArea = inArea,
    keypressed = keypressed,
    active = function() infoBox.active = true end,
    deactive = function() infoBox.active = false end
}